//
//  ImageSaveViewController.m
//  BeautyClother
//
//  Created by dllo on 15/8/29.
//  Copyright (c) 2015年 宋爽. All rights reserved.
//

#import "ImageSaveViewController.h"

@interface ImageSaveViewController ()

@property (nonatomic, retain) UIImageView *imageview;
@property (nonatomic, assign) CGFloat realHeight;
@property (nonatomic, assign) CGFloat offHeigt;
@property (nonatomic, assign) CGFloat offWidth;

@end

@implementation ImageSaveViewController

- (void)dealloc
{
    [_imageview release];
    [super dealloc];
    
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self createButton];
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    self.offHeigt = [[UIScreen mainScreen]bounds].size.height / 667;
    self.offWidth = [[UIScreen mainScreen]bounds].size.width / 375;
    // Do any additional setup after loading the view.
}

- (void)createButton
{
    if (self.view.frame.size.width * self.height / self.width > self.view.frame.size.height - 140* _offHeigt) {
        self.realHeight = self.view.frame.size.height - 140* _offHeigt;
    }else
    {
        self.realHeight = self.view.frame.size.width * self.height / self.width;
    }
    self.imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, (self.view.frame.size.height - self.realHeight) / 2 , self.view.frame.size.width, self.realHeight)];
    [self.view addSubview:self.imageview];
    self.imageview.image = self.image;
    self.imageview.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(back:)];
    [self.imageview addGestureRecognizer:tap];
    [tap release];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake((self.view.frame.size.width - 50), self.imageview.frame.size.height + (self.view.frame.size.height - self.realHeight) / 2, 40 , 20 );
    button.backgroundColor = [UIColor lightGrayColor];
    [button setTitle:@"save" forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(keepImage:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)back:(UITapGestureRecognizer *)tap
{
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];

}

- (void)keepImage:(UIButton *)button
{
    UIImageWriteToSavedPhotosAlbum(self.imageview.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}


- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    
    if (error != NULL) {
        UIAlertView *photoSave = [[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"%@",error] delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
        [photoSave show];
        [photoSave dismissWithClickedButtonIndex:0 animated:YES];
        [photoSave release];
        photoSave = nil;
    }else {
        UIAlertView *photoSave = [[UIAlertView alloc] initWithTitle:@"保存成功" message:@"图片保存到本地" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
        [photoSave show];
        [photoSave dismissWithClickedButtonIndex:0 animated:YES];
        [photoSave release];
        photoSave = nil;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
